package patrol.mobile.com.mobilepatrol.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.bean.RouteSurveyTaskDataBO;
import patrol.mobile.com.mobilepatrol.widget.MyEditText;

/**
 * Created by long on 2016/8/24.
 * 桥梁巡查列表
 */
public class RoadEditAdapter extends BaseAdapter {
    private BaseActivity context;
    private  List<RouteSurveyTaskDataBO> data;
    private int pos;
    private MyEditText mEditText;
    private String surveyType;
    public RoadEditAdapter(Context context, List<RouteSurveyTaskDataBO> data,int pos,String surveyType) {
        this.data = data;
        this.context = (BaseActivity) context;
        this.pos = pos+1;
        this.surveyType = surveyType;
    }

    public void updateList(int pos){
        this.pos = pos+1;
        notifyDataSetChanged();
    }
    public List<RouteSurveyTaskDataBO> save(){
        return data;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = View.inflate(context,R.layout.item_edit,null);
        TextView tvname = (TextView) convertView.findViewById(R.id.tv_name);
        TextView tv_unit = (TextView) convertView.findViewById(R.id.tv_unit);
        mEditText = (MyEditText) convertView.findViewById(R.id.et);
        TextView tv_check = convertView.findViewById(R.id.tv_check);

        final RouteSurveyTaskDataBO bean = data.get(position);
        tvname.setText(bean.columnName);
        if (bean.nickName!=null&&!bean.nickName.equals("")){
            tv_unit.setText("("+bean.nickName+")"+bean.unit);
        }else{
            tv_unit.setText(bean.unit);
        }

        if (surveyType.equals("调查")){
            tv_check.setVisibility(View.GONE);
        }else{
            tv_check.setVisibility(View.VISIBLE);
            switch (pos){
                case 1:
                    tv_check.setText(bean.getSurveyOne()+"");
                    break;
                case 2:
                    tv_check.setText(bean.getSurveyTwo()+"");
                    break;
                case 3:
                    tv_check.setText(bean.getSurveyThree()+"");
                    break;
                case 4:
                    tv_check.setText(bean.getSurveyFour()+"");
                    break;
                case 5:
                    tv_check.setText(bean.getSurveyFive()+"");
                    break;
                case 6:
                    tv_check.setText(bean.getSurveySix()+"");
                    break;
                case 7:
                    tv_check.setText(bean.getSurveySeven()+"");
                    break;
                case 8:
                    tv_check.setText(bean.getSurveyEight()+"");
                    break;
                case 9:
                    tv_check.setText(bean.getSurveyNine()+"");
                    break;
                case 10:
                    tv_check.setText(bean.getSurveyTen()+"");
                    break;
            }
        }
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (!s.toString().contains("+")){
                    }else{
                        String[] s_arr = s.toString().split("\\+");
                        if (s_arr.length==1){
                            s = s_arr[0];
                        }else{
                            s = String.valueOf(Double.valueOf(s_arr[0])+Double.valueOf(s_arr[1]));
                        }

                    }
                if (surveyType.equals("调查")){
                    switch (pos){
                        case 1:
                            bean.setSurveyOne(Double.valueOf(s.toString()));
                            break;
                        case 2:
                            bean.setSurveyTwo(Double.valueOf(s.toString()));
                            break;
                        case 3:
                            bean.setSurveyThree(Double.valueOf(s.toString()));
                            break;
                        case 4:
                            bean.setSurveyFour(Double.valueOf(s.toString()));
                            break;
                        case 5:
                            bean.setSurveyFive(Double.valueOf(s.toString()));
                            break;
                        case 6:
                            bean.setSurveySix(Double.valueOf(s.toString()));
                            break;
                        case 7:
                            bean.setSurveySeven(Double.valueOf(s.toString()));
                            break;
                        case 8:
                            bean.setSurveyEight(Double.valueOf(s.toString()));
                            break;
                        case 9:
                            bean.setSurveyNine(Double.valueOf(s.toString()));
                            break;
                        case 10:
                            bean.setSurveyTen(Double.valueOf(s.toString()));
                            break;
                    }
                }else{
                    switch (pos){
                        case 1:
                            bean.setCheckOne(Double.valueOf(s.toString()));
                            break;
                        case 2:
                            bean.setCheckTwo(Double.valueOf(s.toString()));
                            break;
                        case 3:
                            bean.setCheckThree(Double.valueOf(s.toString()));
                            break;
                        case 4:
                            bean.setCheckFour(Double.valueOf(s.toString()));
                            break;
                        case 5:
                            bean.setCheckFive(Double.valueOf(s.toString()));
                            break;
                        case 6:
                            bean.setCheckSix(Double.valueOf(s.toString()));
                            break;
                        case 7:
                            bean.setCheckSeven(Double.valueOf(s.toString()));
                            break;
                        case 8:
                            bean.setCheckEight(Double.valueOf(s.toString()));
                            break;
                        case 9:
                            bean.setCheckNine(Double.valueOf(s.toString()));
                            break;
                        case 10:
                            bean.setCheckTen(Double.valueOf(s.toString()));
                            break;
                    }
                }

            }
            @Override
            public void afterTextChanged(Editable s) { }
        });
        if (surveyType.equals("调查")){
            switch (pos){
                case 1:
                    mEditText.setText(bean.getSurveyOne()+"");

                    break;
                case 2:
                    mEditText.setText(bean.getSurveyTwo()+"");
                    break;
                case 3:
                    mEditText.setText(bean.getSurveyThree()+"");
                    break;
                case 4:
                    mEditText.setText(bean.getSurveyFour()+"");
                    break;
                case 5:
                    mEditText.setText(bean.getSurveyFive()+"");
                    break;
                case 6:
                    mEditText.setText(bean.getSurveySix()+"");
                    break;
                case 7:
                    mEditText.setText(bean.getSurveySeven()+"");
                    break;
                case 8:
                    mEditText.setText(bean.getSurveyEight()+"");
                    break;
                case 9:
                    mEditText.setText(bean.getSurveyNine()+"");
                    break;
                case 10:
                    mEditText.setText(bean.getSurveyTen()+"");
                    break;
            }
        }else{
            switch (pos){
                case 1:
                    mEditText.setText(bean.getCheckOne()+"");

                    break;
                case 2:
                    mEditText.setText(bean.getCheckTwo()+"");
                    break;
                case 3:
                    mEditText.setText(bean.getCheckThree()+"");
                    break;
                case 4:
                    mEditText.setText(bean.getCheckFour()+"");
                    break;
                case 5:
                    mEditText.setText(bean.getCheckFive()+"");
                    break;
                case 6:
                    mEditText.setText(bean.getCheckSix()+"");
                    break;
                case 7:
                    mEditText.setText(bean.getCheckSeven()+"");
                    break;
                case 8:
                    mEditText.setText(bean.getCheckEight()+"");
                    break;
                case 9:
                    mEditText.setText(bean.getCheckNine()+"");
                    break;
                case 10:
                    mEditText.setText(bean.getCheckTen()+"");
                    break;
            }
        }

        return convertView;
    }
    public void clearFocus(){
        if (mEditText.isFocused()){
            mEditText.clearFocus();
        }
    }
}
